package com.atguigu.gmall.product.controller;

import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.model.product.*;
import com.atguigu.gmall.product.mapper.SpuImageMapper;
import com.atguigu.gmall.product.service.BaseSaleAttrService;
import com.atguigu.gmall.product.service.SpuImageService;
import com.atguigu.gmall.product.service.SpuSaleAttrService;
import com.atguigu.gmall.product.service.SpuService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/admin/product")
///admin/product/baseSaleAttrList
public class SpuManageController {

    @Autowired
    private SpuService spuService;

    @Autowired
    private BaseSaleAttrService baseSaleAttrService;

    @Autowired
    private SpuSaleAttrService spuSaleAttrService;

    @Autowired
    private SpuImageService spuImageService;


    // /admin/product/{page}/{limit}
    // 获取spu分类列表
    @GetMapping("{page}/{limit}")
    public Result getPage(@PathVariable Long page, @PathVariable Long limit, SpuInfo spuInfo){

        Page<SpuInfo> spuInfoPage = new Page<>(page, limit);

        IPage<SpuInfo> page1 = spuService.getPage(spuInfoPage, spuInfo.getCategory3Id());
        return Result.ok(page1);
    }


    // /admin/product/saveSpuInfo
    // 保存spu
    @PostMapping("saveSpuInfo")
    public Result saveSpuInfo(@RequestBody SpuInfo spuInfo){

        // 调用服务层接口
        spuService.saveSpuInfo(spuInfo);

        // 返回结果
        return Result.ok();
    }

    // baseSaleAttrList
    // 获取销售属性值
    @GetMapping("baseSaleAttrList")
    public Result baseSaleAttrList(){

        List<BaseSaleAttr> list = baseSaleAttrService.baseSaleAttrList();

        return Result.ok(list);
    }

    // http://localhost/admin/product/getSpuInfo/6
    // spu 修改数据前回显
    @GetMapping("getSpuInfo/{id}")
    public Result getSpuInfo(@PathVariable Long id){

        // 调用服务层接口
        SpuInfo spuInfo = spuService.getSpuInfo(id);

        //返回结果
        return Result.ok(spuInfo);
    }


    // updateSpuInfo
    // 修改商品信息
    @PostMapping("updateSpuInfo")
    public Result updateSpuInfo(@RequestBody SpuInfo spuInfo){

        // 调用service
        spuService.updateSpuInfo(spuInfo);

        // 返回结果
        return Result.ok();
    }

    // 根据spuid查询销售属性
    // http://localhost/admin/product/spuSaleAttrList/8
    @GetMapping("spuSaleAttrList/{spuId}")
    public Result spuSaleAttrList(@PathVariable Long spuId){

        // 调用service服务层，查询数据
        List<SpuSaleAttr> spuSaleAttrList = spuSaleAttrService.spuSaleAttrList(spuId);

        //返回数据
        return Result.ok(spuSaleAttrList);
    }


    // /admin/product/spuImageList/{spuId}
    // 根据spuid查询spu images集合
    @GetMapping("spuImageList/{spuId}")
    public Result spuImageList(@PathVariable Long spuId){
        // 调用服务层
        List<SpuImage> spuImages = spuImageService.spuImageList(spuId);
        // 返回数据
        return Result.ok(spuImages);
    }

}
